<template>
	<el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable width="680px">
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="24" class="mb20">
					<el-form-item label="归属单位" prop="deptId">
						<el-tree-select
							v-model="form.deptId"
							:data="parentData"
							:props="{ value: 'id', label: 'name', children: 'children', disabled: 'isLock' }"
							clearable
							filterable
							check-strictly
							:render-after-expand="false"
							:disabled="!!form.id"
							placeholder="请选择授权单位"
							ref="treeRef"
						/>
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20">
					<el-form-item label="姓名" prop="name">
						<el-input v-model="form.name" placeholder="请输入姓名" maxlength="50" />
					</el-form-item>
				</el-col>
				<el-col :span="24" class="mb20">
					<el-form-item label="接警号码" prop="phone">
						<el-input v-model="form.phone" maxlength="11" placeholder="请输入接警号码" />
						<div class="text-error">请您确保填写的接警号码真实有效，填入未经授权使用的号码带来的法律责任需自行承担</div>
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="报警方式">
						<el-checkbox v-model="form.smsAlarm" label="短信" :true-label="20" :false-label="10" border />
						<el-checkbox v-model="form.telephoneAlarm" label="电话" :true-label="20" :false-label="10" border />
						<el-checkbox v-model="form.officialAlarm" label="微信公众号" :true-label="20" :false-label="10" border />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="头衔" prop="title">
						<el-input v-model="form.title" placeholder="请输入头衔" maxlength="50" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="邮箱" prop="email">
						<el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" />
					</el-form-item>
				</el-col>

				<el-col :span="24" class="mb20">
					<el-form-item label="备注" prop="remarks">
						<el-input v-model="form.remarks" placeholder="请输入备注" maxlength="50" />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="BasicAlarmContactsDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from '/@/hooks/message';
import { getObj, addObj, putObj } from '/@/api/installation/basicAlarmContacts';
import { rule } from '/@/utils/validate';
import { deptTree } from '/@/api/admin/dept';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
const parentData = ref([]);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	name: '',
	deptId: '',
	phone: '',
	title: '',
	email: '',
	telephoneAlarm: 10,
	smsAlarm: 10,
	officialAlarm: 10,
	remarks: '',
});

// 定义校验规则
const dataRules = ref({
	deptId: [{ required: true, message: '归属单位不能为空', trigger: 'blur' }],
	name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
	phone: [
		{ required: true, message: '接警号码不能为空', trigger: 'blur' },
		{
			validator: rule.mobilePhone,
			trigger: 'blur',
		},
	],
	email: [
		{
			validator: rule.email,
			trigger: 'blur',
		},
	],
});

// 打开弹窗
const openDialog = (id: string) => {
	visible.value = true;
	form.id = '';
	form.telephoneAlarm = 10;
	form.smsAlarm = 10;
	form.officialAlarm = 10;

	// 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

	// 获取basicAlarmContacts信息
	if (id) {
		form.id = id;
		getbasicAlarmContactsData(id);
	}
	getDeptData();
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		form.id ? await putObj(form) : await addObj(form);
		useMessage().success(form.id ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 初始化表单数据
const getbasicAlarmContactsData = (id: string) => {
	// 获取数据
	loading.value = true;
	getObj(id)
		.then((res: any) => {
			Object.assign(form, res.data);
		})
		.finally(() => {
			loading.value = false;
		});
};

const getDeptData = async () => {
	deptTree().then((res) => {
		parentData.value = res.data;
	});
};
// 暴露变量
defineExpose({
	openDialog,
});
</script>
